import 'package:animated_background/animated_background.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:hc_bot_pro/common/tool/string_ex.dart';
import 'package:text_scroll/text_scroll.dart';

import '../../common/model/channel.dart';
import '../../common/service/channel/channel.dart';

class BackgroundView extends StatefulWidget {
  final String tip;
  final Widget child;
  const BackgroundView({super.key, required this.child, required this.tip});

  @override
  State<BackgroundView> createState() => _BackgroundViewState();
}

class _BackgroundViewState extends State<BackgroundView>
    with TickerProviderStateMixin {
  @override
  Widget build(BuildContext context) {
    Channel? channel = Get.find<ChannelService>().channel;
    String bg = channel?.appStyle.playImages.backImage ?? "";
    return Container(
      decoration: BoxDecoration(
        image: DecorationImage(
          fit: BoxFit.cover,
          image: bg.isEmpty
              ? const AssetImage("assets/images/bg_main.png")
              : CachedNetworkImageProvider(bg),
        ),
      ),
      child: Stack(
        children: [
          AnimatedBackground(
            behaviour: RandomParticleBehaviour(
              options: ParticleOptions(
                particleCount: 25,
                spawnMaxSpeed: 75,
                spawnMinSpeed: 25,
                baseColor: channel == null
                    ? Colors.white
                    : channel.appStyle.colors.playRollBarColor.toColor(),
              ),
            ),
            vsync: this,
            child: widget.child,
          ),
          Positioned(
            top: kToolbarHeight,
            child: Container(
              padding: EdgeInsets.symmetric(vertical: 15.h),
              child: TextScroll(
                "                                                                                                                        " +
                    widget.tip +
                    "                                                                                            ",
                style: TextStyle(
                  fontSize: 15.sp,
                  color: channel == null
                      ? Colors.black
                      : channel.appStyle.colors.playTitleColor.toColor(),
                ),
              ),
            ),
            left: 0,
            right: 0,
          ),
        ],
      ),
    );
  }
}
